package com.bird.generate.ddd.domain.generateutil

import cn.hutool.core.io.FileUtil
import com.bird.generate.ddd.domain.*

class LocalGenerate {

    companion object {
        /**
         * 生成local代码
         */
        fun adapterGenerate(path: String,aggregateRoot: AggregateRootEntity) {
            var localRootPath = path.appendNextPath(aggregateRoot.moduleName.lowercase()).appendNextPath("${aggregateRoot.moduleName.lowercase()}bc-northbound").appendNextPath("${aggregateRoot.moduleName.lowercase()}bc-northbound-local").appendNextPath(JAVA_PATH)
                .appendNextPath("${aggregateRoot.comPrefix.replace(".","/")}/${aggregateRoot.projectName.lowercase()}prd/${aggregateRoot.moduleName.lowercase()}bc/northbound/local/${aggregateRoot.enName.lowercase()}biz")
            //root 包路径
            var localJavaPath = "${aggregateRoot.comPrefix.replace(".","/")}/${aggregateRoot.projectName.lowercase()}prd/${aggregateRoot.moduleName.lowercase()}bc/northbound/local/${aggregateRoot.enName.lowercase()}biz".replace("/", ".")

            //创建两个pom.xml
            generateNorthboundPomXml(aggregateRoot,path.appendNextPath(aggregateRoot.moduleName.lowercase()+"bc").appendNextPath("${aggregateRoot.moduleName.lowercase()}bc-northbound"))
            generateLocalPomXml(aggregateRoot,path.appendNextPath(aggregateRoot.moduleName.lowercase()+"bc").appendNextPath("${aggregateRoot.moduleName.lowercase()}bc-northbound").appendNextPath("${aggregateRoot.moduleName.lowercase()}bc-northbound-local"))
            //创建local文件夹
            generateLocalFolder(localRootPath)
            //创建包路径
            generatePackageInfo(localRootPath.substringBeforeLast("/"), localJavaPath.substringBeforeLast("."))

        }

        private fun generateLocalFolder(path: String) {
            FileUtil.mkdir(path)
        }


        private fun generateLocalPomXml(
            aggregateRoot: AggregateRootEntity,
            path: String
        ) {
            val xmlContent =
"""<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>${aggregateRoot.comPrefix}</groupId>
        <artifactId>${aggregateRoot.moduleName.lowercase()}bc-northbound</artifactId>
        <version>${aggregateRoot.version}</version>
    </parent>

    <artifactId>${aggregateRoot.moduleName.lowercase()}bc-northbound-local</artifactId>

    <dependencies>
        <dependency>
            <groupId>${aggregateRoot.comPrefix}</groupId>
            <artifactId>${aggregateRoot.moduleName.lowercase()}bc-domain</artifactId>
            <version>${'$'}{zz.project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.zz</groupId>
            <artifactId>zz-starter-mybatis</artifactId>
        </dependency>
    </dependencies>

</project>
                """
            createFile(path.appendNextPath("pom.xml"), xmlContent)
        }

        private fun generateNorthboundPomXml(
            aggregateRoot: AggregateRootEntity,
            path: String
        ) {
            val xmlContent =
"""<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>${aggregateRoot.comPrefix}</groupId>
        <artifactId>${aggregateRoot.moduleName.lowercase()}bc</artifactId>
        <version>${aggregateRoot.version}</version>
    </parent>
    <artifactId>${aggregateRoot.moduleName.lowercase()}bc-northbound</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>${aggregateRoot.moduleName.lowercase()}bc-northbound-local</module>
        <module>${aggregateRoot.moduleName.lowercase()}bc-northbound-remote</module>
    </modules>


</project>
           
            """
            createFile(path.appendNextPath("pom.xml"), xmlContent)
        }


    }

}